from upload_common import read_data_from_stdin
from upload_common import data_is_http_link
from upload_common import write_data_to_stdout
from upload_common import data_is_filename
from bs4 import BeautifulSoup

data = read_data_from_stdin()
if data_is_http_link(data):
    write_data_to_stdout(data)
elif data_is_filename(data):
    write_data_to_stdout(data)
else:
    try:
        html_content = data.strip().decode("utf-8")
        soup = BeautifulSoup(html_content, 'html.parser')
        img_tag = soup.find('img')
        if img_tag and img_tag.has_attr('src'):
            write_data_to_stdout(img_tag.encode("utf-8"))
        else:
            write_data_to_stdout(data)
    except Exception as e:
        write_data_to_stdout(data)
